from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet
from apps.utils.pages import MyPage
from .models import Spit
from .serializers import SpitSerializer
from django.views import View
from django.http import JsonResponse
from rest_framework.views import APIView
from rest_framework.response import Response
from apps.users.models import User

# Create your views here.


# 列表
class SpitListView(APIView):
    # queryset = Spit.objects.all()
    # serializer_class = SpitSerializer
    # pagination_class = MyPage
    def get(self,request):
        spits = Spit.objects.filter(parent__isnull=True)
        serializer = SpitSerializer(spits, many=True)
        list = serializer.data
        return Response(list)


    # 发布吐槽
    def post(self, request):
        dict = request.data
        content = dict.get('content')
        if content is None:
            return None
        parent_id = dict.get('parent')

        if request.user.is_anonymous:
            if parent_id is None:
                Spit.objects.create(userid='anonymous', content=content)
                return Response('success')
            else:
                return Response()

        try:
            user = User.objects.get(username=request.user.username)
            avatar = user.avatar
            spit = Spit.objects.create(userid=user.id,
                                nickname=user.username,
                                avatar=avatar,
                                content=content,
                                parent_id=int(parent_id))
            if parent_id:
                spit = Spit.objects.get(id=parent_id)
                spit.conment += 1
                spit.save()
        except Exception as e:
            return Response()

        return Response(spit)



# 收藏或取消收藏
class SpitCollectView(View):
    def put(self, request, id):
        spits = Spit.objects.filter(collected=True)
        collect_list = []
        for item in spits:
            collect_list.append(item.id)
        try:
            if id in collect_list:
                spit = Spit.objects.get(pk=id)
                spit.collected = False
            else:
                spit = Spit.objects.get(pk=id)
                spit.collected = True
            spit.save()
        except Exception as e:
            return JsonResponse({'message': 200, 'success': 0})

        return JsonResponse({'message': 200, 'success': 1})


# 点赞或取消点赞
class SpitHasthumbupView(View):
    def put(self, request, id):
        spits = Spit.objects.filter(hasthumbup=True)
        hasthumbup_list = []
        for item in spits:
            hasthumbup_list.append(item.id)
        try:
            if id in hasthumbup_list:
                spit = Spit.objects.get(pk=id)
                spit.thumbup -= 1
                spit.hasthumbup = False
            else:
                spit = Spit.objects.get(pk=id)
                spit.thumbup +=1
                spit.hasthumbup = True
            spit.save()
        except Exception as e:
            return JsonResponse({'message': 200, 'success': 0})

        return JsonResponse({'message': 200, 'success': 1})


# 吐槽详情
class SpitDetailView(APIView):
    def get(self,request, id):
        spit = Spit.objects.get(id=id)
        serializer = SpitSerializer(spit)
        dict = serializer.data
        return Response(dict)



# 吐槽评论
class SpitCommentView(APIView):
    def get(self,request,id):
        spits = Spit.objects.filter(parent_id=id)
        serializer = SpitSerializer(spits,many=True)
        list = serializer.data
        return Response(list)
